package com.hangzhou.welbeing.welbeinginstrument.fragement;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.activity.TaoBaoActivity;
import com.hangzhou.welbeing.welbeinginstrument.adapter.SelectedRecyclerAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.WeChatMallBeanDate;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.utils.PackageUtils;
import com.youth.banner.Banner;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司16
 * Created by 20433 on 2018/1/29
 */

public class WeChatMallFragment extends Fragment {
    @Bind(R.id.fragment_find_rl_man_near)
    RelativeLayout fragmentFindRlManNear;
    @Bind(R.id.fragment_wechat_mall_picter)
    Banner fragmentWechatMallPicter;
    @Bind(R.id.fragment_wechat_mall_baseRecyclerView)
    RecyclerView fragmentWechatMallBaseRecyclerView;
    @Bind(R.id.fragment_wechat_mall_text)
    TextView fragmentWechatMallText;
    private Context mcontext;
    private SelectedRecyclerAdapter selectedRecyclerAdapter;
    private GridLayoutManager gridLayoutManager;
    private int p = 1;
    private List<WeChatMallBeanDate.DataBean> mlistDateBean = new ArrayList<>();
    private PopupWindow popupWindow;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mcontext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(mcontext, R.layout.fragment_wechat_mall, null);
        ButterKnife.bind(this, view);
        showProgress("加载中...");
        gridLayoutManager = new GridLayoutManager(mcontext,2);
        fragmentWechatMallBaseRecyclerView.setLayoutManager(gridLayoutManager);
        selectedRecyclerAdapter = new SelectedRecyclerAdapter(mcontext);
        fragmentWechatMallBaseRecyclerView.setNestedScrollingEnabled(false);
        fragmentWechatMallBaseRecyclerView.setAdapter(selectedRecyclerAdapter);
        loadDate(p);
        selectedRecyclerAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) {
               // Log.e("tag","+++++loadDate+++=====================++++");
               int postion =  fragmentWechatMallBaseRecyclerView.getChildAdapterPosition(view);
                if (PackageUtils.checkPackage(mcontext, "com.taobao.taobao")){
                    Intent  localIntent = new Intent();
                    localIntent.setAction("android.intent.action.VIEW");
                    localIntent.setData(Uri.parse(mlistDateBean.get(postion).con));
                    WeChatMallFragment.this.startActivity(localIntent);
                    return;
                }
                    Intent localIntent = new Intent(mcontext, TaoBaoActivity.class);
                    localIntent.putExtra("strcon", mlistDateBean.get(postion).con);
                    startActivity(localIntent);

            }

            @Override
            public void onItemFirstButtonClick(View v, int position) {

            }
        });
        return view;
    }

    private void loadDate(int p) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.TAO_BAO)
                .addParams("p", p+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse =  response.body().string();
                        Gson gson = new Gson();

                        final WeChatMallBeanDate weChatMallBeanDate =   gson.fromJson(strresponse, WeChatMallBeanDate.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(weChatMallBeanDate.errorCode == 200){
                                    popupWindow.dismiss();
                                    List<WeChatMallBeanDate.DataBean> mlist = weChatMallBeanDate.data;
                                    if(mlist != null && mlist.size() > 0){
                                        mlistDateBean.addAll(mlist);
                                        selectedRecyclerAdapter.addItem(mlist);
                                    }
                                }else{
                                    popupWindow.dismiss();
                                    Toast.makeText(mcontext,weChatMallBeanDate.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        popupWindow.dismiss();
                        Toast.makeText(mcontext,"请检查网络", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    /**
     * @desc 显示进度
     * */
    private void showProgress(String str) {
        popupWindow = new PopupWindow(mcontext);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(mcontext).inflate(R.layout.layout_show_progress_popupwindow_style,null);
        TextView tv_content = (TextView) view.findViewById(R.id.layout_show_progress_popupwindow_style_tv_content);
        tv_content.setText(str);
        popupWindow.setContentView(view);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);
        View rootview = View.inflate(mcontext,R.layout.activit_release_similar_profile_page_focused,null);
        popupWindow.showAtLocation(rootview,  Gravity.CENTER,0,0);

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        popupWindow.dismiss();
        ButterKnife.unbind(this);
    }
}
